#include "ros/ros.h"
#include "sensor_msgs/Image.h"
#include "sensor_msgs/image_encodings.h"


#include <ros/ros.h>
#include <nodelet/nodelet.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include <boost/thread.hpp>

namespace depth_image_proc {

namespace enc = sensor_msgs::image_encodings;

class ConvertMetricNodelet : public nodelet::Nodelet
{
  // Subscriptions
  boost::shared_ptr<image_transport::ImageTransport> it_;
  
  // 订阅raw
  image_transport::Subscriber sub_raw_;

  // Publications
  // 发布
  boost::mutex connect_mutex_;
  image_transport::Publisher pub_depth_;

  virtual void onInit();

  void connectCb();

  void depthCb(const sensor_msgs::ImageConstPtr& raw_msg);
};

// 类初始化
void ConvertMetricNodelet::onInit()
{
  ros::NodeHandle& nh = getNodeHandle();
  it_.reset(new image_transport::ImageTransport(nh));

  // Monitor whether anyone is subscribed to the output
  image_transport::SubscriberStatusCallback connect_cb = boost::bind(&ConvertMetricNodelet::connectCb, this);
  // Make sure we don't enter connectCb() between advertising and assigning to pub_depth_
  boost::lock_guard<boost::mutex> lock(connect_mutex_);
  // 发布节点初始化
  pub_depth_ = it_->advertise("image", 1, connect_cb, connect_cb);
}

// Handles (un)subscribing when clients (un)subscribe
void ConvertMetricNodelet::connectCb()
{
  boost::lock_guard<boost::mutex> lock(connect_mutex_);
  if (pub_depth_.getNumSubscribers() == 0)
  {
    sub_raw_.shutdown();
  }
  else if (!sub_raw_)
  {
    image_transport::TransportHints hints("raw", ros::TransportHints(), getPrivateNodeHandle());
    // 订阅并产生回调函数
    sub_raw_ = it_->subscribe("image_raw", 1, &ConvertMetricNodelet::depthCb, this, hints);
  }
}

void ConvertMetricNodelet::depthCb(const sensor_msgs::ImageConstPtr& raw_msg)
{
  // Allocate new Image message 初始化一个
  sensor_msgs::ImagePtr depth_msg( new sensor_msgs::Image );
  depth_msg->header   = raw_msg->header;
  depth_msg->height   = raw_msg->height;
  depth_msg->width    = raw_msg->width;

  // Set data, encoding and step after converting the metric. 判断传进来的type
  if (raw_msg->encoding == enc::TYPE_16UC1)
  {
    depth_msg->encoding = enc::TYPE_32FC1;

    depth_msg->step     = raw_msg->width * (enc::bitDepth(depth_msg->encoding) / 8);
    depth_msg->data.resize(depth_msg->height * depth_msg->step);
    
    // 毫米转米
    // Fill in the depth image data, converting mm to m
    float bad_point = std::numeric_limits<float>::quiet_NaN (); // NaN值为坏点

    // 定义一个指针指向raw的地一个数据
    const uint16_t* raw_data = reinterpret_cast<const uint16_t*>(&raw_msg->data[0]);
    
    // depth值的指针为float
    float* depth_data = reinterpret_cast<float*>(&depth_msg->data[0]);
    for (unsigned index = 0; index < depth_msg->height * depth_msg->width; ++index)
    {
      uint16_t raw = raw_data[index];  // 指针遍历raw的值
      depth_data[index] = (raw == 0) ? bad_point : (float)raw * 0.001f;
    }
  }
  else if (raw_msg->encoding == enc::TYPE_32FC1)
  {
    depth_msg->encoding = enc::TYPE_16UC1;
    depth_msg->step     = raw_msg->width * (enc::bitDepth(depth_msg->encoding) / 8);
    depth_msg->data.resize(depth_msg->height * depth_msg->step);
    // Fill in the depth image data, converting m to mm 已经修改，没有转为毫米
    uint16_t bad_point = 0;
    const float* raw_data = reinterpret_cast<const float*>(&raw_msg->data[0]);
    uint16_t* depth_data = reinterpret_cast<uint16_t*>(&depth_msg->data[0]);
    for (unsigned index = 0; index < depth_msg->height * depth_msg->width; ++index)
    {
      float raw = raw_data[index];
      depth_data[index] = std::isnan(raw) ? bad_point : (uint16_t)(raw * 1000);
    }
  }
  else
  {
    ROS_ERROR("Unsupported image conversion from %s.", raw_msg->encoding.c_str());
    return;
  }

  pub_depth_.publish(depth_msg);
}

} // namespace depth_image_proc